// 电影库界面
var express = require('express');
var router = express.Router();
const sql = require('../conf/sql')
const pool = require('../conf/db')
const variable = require('../utils/global_variable')

//电影库分类查询
router.post('/movie', function (req, res, next) {
  //此时是20条数据
  //注意page是number类型，前端传入的是string，所以要转换
 
//  console.log(req.body);
  pool.query(
    `select * from movie where mcategory like '%${req.body.mcategory}%' 
    and mtype = 1 order by ${req.body.option} desc limit ${req.body.page}, 5`,
    (err, result) => {
    if (err) {
      console.log(err);
      next(err)
      return;
    }
    for (const i of result) {
      i.mimage = variable.prefix + i.mimage + variable.suffix  
      i.mshowdate = i.mshowdate.getFullYear()
    }
    if (result.length == 0) {
      res.json({ code: 201, msg: '请求失败' });
      return;
    } else {
      res.json({ code: 200, msg: '请求成功', data: result });

    }
  })

});

//电视剧分类查询
router.get('/teleplay', function (req, res, next) {
  //此时是20条数据
  //注意page是number类型，前端传入的是string，所以要转换
  pool.query(
    `select * from movie where mcategory like '%${req.body.mcategory}%' 
    and mtype = 0 order by ${req.body.option} desc limit ${req.body.page}, 10`,
    (err, result) => {
      
    if (err) {
      console.log(err);
      next(err)
      return;
    }
    console.log(result.length);
    if (result.length == 0) {
      res.json({ code: 201, msg: '请求失败' });
      return;
    } else {
      res.json({ code: 200, msg: '请求成功', data: result });

    }
  })
});

//请求点击率前十的电影
router.get('/rate', function (req, res, next) {
  pool.query(sql.queryMoreTenMovie, (err, result) => {
    if (err) {
      console.log(err);
      next(err)
      return;
    }
    for (const i of result) {
      i.mimage = variable.prefix + i.mimage + variable.suffix  
    }
   
    if (result.length == 0) {
      res.json({ code: 201, msg: '请求失败' });
      return;
    } else {
      res.json({ code: 200, msg: '请求成功', data: result });

    }
  })
});
module.exports = router;